<template>
  <el-drawer 
   v-model="visibleDrawer" 
   size="50%" 
   :with-header="false"
   :destroy-on-close="true">
    <el-form :model="formModel" ref="formRef">
      <el-form-item label="题目标题:">
        <el-input 
         style="width:387px !important" 
         v-model="formQuestion.title" 
         placeholder="请输入标题">
        </el-input>
      </el-form-item>
      <el-form-item label="题目难度:">
        <question-selector 
         style="width:387px !important" 
         v-model="formQuestion.difficulty" 
         width="100%" placeholder="请选择题目难度:">
        </question-selector>
      </el-form-item>
      <el-form-item label="时间限制（单位毫秒）:">
        <el-input 
         style="width:300px !important" 
         v-model="formQuestion.timeLimit" 
         placeholder="请输入时间限制">
        </el-input>
      </el-form-item>
      <el-form-item label="空间限制（单位字节）:">
        <el-input 
         style="width:300px !important" 
         v-model="formQuestion.spaceLimit" 
         placeholder="请输入空间限制">
        </el-input>
      </el-form-item>
      <el-form-item label="题目内容:">
        <div class="editor">
          <quill-editor 
           placeholder="请输入题目内容" 
           v-model:content="formQuestion.content"
           content-type="html">
          </quill-editor>
        </div>
      </el-form-item>
      <el-form-item label="题目用例:">
        <el-input 
         style="width:387px !important" 
         v-model="formQuestion.questionCase" 
         placeholder="请输入题目用例">
        </el-input>
      </el-form-item>
      <el-form-item label="默认代码块:">
        <code-editor 
         ref="defaultCodeRef" 
         :defaultCode="formQuestion.defaultCode" 
         @update:value="handleEditorContent">
        </code-editor>
      </el-form-item>
      <el-form-item label="main函数:">
        <code-editor 
         ref="mainCodeRef" 
         :defaultCode="formQuestion.mainFunc" 
         @update:value="handleEditorMainFunc">
        </code-editor>
      </el-form-item>
      <el-form-item>
        <el-button 
         class="question-button" 
         type="primary" 
         plain 
         @click="onSubmit()">发布</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>
<script setup>
  import { QuillEditor } from '@vueup/vue-quill';
  import '@vueup/vue-quill/dist/vue-quill.snow.css'
  import CodeEditor from './CodeEditor.vue';
  import { ref, reactive } from 'vue';
  import QuestionSelector from './QuestionSelector.vue';
  import { ElMessage } from 'element-plus';
  import { addQuestionService, editQuestionService, getQuestionDetailService } from '@/api/question';

  const visibleDrawer = ref(false)

  const defaultCodeRef  = ref(null)

  const mainCodeRef = ref(null)

  //抽屉组件作为子组件时向父组件暴露的方法，父组件通过ref属性获取到子组件实例进而调用方法
  //确保defineExpose向外暴露的内容在其之前声明

  const open = async questionId => {
    visibleDrawer.value = true
    for (const key in formQuestion) {
      if (formQuestion.hasOwnProperty(key)) {
        formQuestion[key] = '';
      }
    }
    if (questionId) {
      const { data } = await getQuestionDetailService(questionId)
      Object.assign(formQuestion, data)
      defaultCodeRef.value.setCodeEditorContent(formQuestion.defaultCode)
      mainCodeRef.value.setCodeEditorContent(formQuestion.mainFunc)
    }
  }

  defineExpose({
    open
  })

  const emit = defineEmits(['success'])

  const formQuestion = reactive({
    questionId: '',
    title: '',
    difficulty: '',
    content: '',
    questionCase: '',
    timeLimit: '',
    spaceLimit: '',
    defaultCode: '',
    mainFunc: ''
  })

  const handleEditorContent = content => formQuestion.defaultCode = content

  const handleEditorMainFunc = content => formQuestion.mainFunc = content

  const validate = () => {
    let msg = ''
    if (!formQuestion.title) {
      msg = '请添加题目标题'
    } else if (formQuestion.difficulty == '') {
      msg = '请选择题目难度'
    } else if (!formQuestion.timeLimit) {
      msg = '请输入时间限制'
    } else if (!formQuestion.spaceLimit) {
      msg = '请输入空间限制'
    } else if (!formQuestion.content) {
      msg = '请输入题目内容'
    } else if (!formQuestion.questionCase) {
      msg = '请输入题目用例'
    } else if (!formQuestion.defaultCode) {
      msg = '请输入默认代码'
    } else if (!formQuestion.mainFunc) {
      msg = '请输入main函数'
    } else {
      msg = ''
    }
    return msg
  }

  const onSubmit = async () => {
    const errorMsg = validate()
    if (errorMsg) {
      ElMessage.error(errorMsg) 
      return false
    }
    const fd = new FormData()
    for (let key in formQuestion) {
      fd.append(key, formQuestion[key])
    }
    if (!formQuestion.questionId) {
      await addQuestionService(fd)
      // await addQuestionService(formQuestion)
      ElMessage.success('添加成功')
      emit('success')
    } else {
      await editQuestionService(fd)
      ElMessage.success('编辑成功')
      emit('success')
    }
    visibleDrawer.value = false 
  }

</script>
<style lang="scss">
    .question-button {
        width: 200px;
    }
</style>